package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.ProductRoute;
import com.efast.dmom.aps.dao.ProductRouteDao;
import com.efast.dmom.aps.entity.ProductRouteEntity;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class ProductRouteDataHandler {

	@Autowired
	private ProductRouteDao productRouteDao;

	public ProductRoute getProductRouteByPartIdAndRouteId(long partId, long routeId) {
		ProductRoute productRoute = new ProductRoute();
		productRoute.setInnerData(productRouteDao.getProductRouteByPartIdAndRouteId(partId,routeId));
		return productRoute;
	}
	
	public ProductRoute getProductRouteRel(String partNo, String partVersion){
		ProductRoute productRoute = new ProductRoute();
		ProductRouteEntity productRouteEntity = productRouteDao.getProductRouteByPartNoAndPartVersion(partNo,partVersion);
		productRoute.setInnerData(productRouteEntity);
		return productRoute;
	}

	public void save(ProductRoute obj) {
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			productRouteDao.save(obj.getInnerData());
		}
	}

	public ProductRoute getById(long id) {
		ProductRoute productRoute = new ProductRoute();
		productRoute.setInnerData(productRouteDao.getById(id));
		return productRoute;
	}

	public void update(ProductRoute obj) {
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			productRouteDao.update(obj.getInnerData());
		}
	}

	public void delete(ProductRoute obj) {
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			productRouteDao.delete(obj.getInnerData());
		}

	}
	public List<ProductRoute> getByPartId(long partId) {
		List<ProductRouteEntity> byPartId = productRouteDao.getByPartId(partId);
		List<ProductRoute> resultList = new ArrayList<ProductRoute>();
		for(ProductRouteEntity entity:byPartId) {
			ProductRoute productRoute = new ProductRoute();
			productRoute.setInnerData(entity);
			resultList.add(productRoute);
		}
		return resultList;
	}

}
